package com.store.app.widget;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.store.app.R;
import com.store.app.bean.BookedBean;
import com.store.app.utils.Utils;

import java.text.DecimalFormat;
import java.util.Collection;
import java.util.Map;

/**
 * Created by Administrator on 2016/3/12.
 */
public class ExchangePopupWindow {
    private PopupWindow popupwindow;
    private Context context;
    private ImageView img;
    private TextView tv_price,tv_kucun,tv_qiding,tv_count_reduce,tv_count_add,tv_buy_count,
            tv_shop_name,tv_phone,tv_sum;
    private Button bt_submit;
    private BookedBean bean;
    public ExchangePopupWindow(BookedBean bean){
        this.bean=bean;
    }
    public PopupWindow getPopupWindow(Context context){

        this.context=context;
        popupwindow = new PopupWindow(context);
        LayoutInflater mLayoutInflater = (LayoutInflater)context. getSystemService(context.LAYOUT_INFLATER_SERVICE);
        View dataSelectView = mLayoutInflater.inflate(
                R.layout.exchange_order_dialog, null);
        // 日期选择器的控件
        img = (ImageView)dataSelectView
                .findViewById(R.id.iv);
        tv_price = (TextView) dataSelectView
                .findViewById(R.id.tv_price);
        tv_kucun = (TextView) dataSelectView.findViewById(R.id.tv_count);
        tv_qiding = (TextView) dataSelectView.findViewById(R.id.tv_start_order_count);
        tv_count_reduce = (TextView) dataSelectView.findViewById(R.id.tv_reduce);
        tv_count_add = (TextView) dataSelectView.findViewById(R.id.tv_add);
        tv_buy_count=(TextView) dataSelectView.findViewById(R.id.tv_buy_count);
        tv_shop_name=(TextView) dataSelectView.findViewById(R.id.tv_shop_name);
        tv_phone=(TextView) dataSelectView.findViewById(R.id.tv_phone);
        tv_sum=(TextView) dataSelectView.findViewById(R.id.tv_sum);
        bt_submit= (Button) dataSelectView.findViewById(R.id.bt_submit);
        String s_prive=bean.getDiscount_price();
        if(!TextUtils.isEmpty(s_prive)){
            try{
                int i_count=(int)(Double.parseDouble(s_prive)*100);
                tv_price.setText(i_count + "");
            }catch (Exception e){
                tv_price.setText(s_prive + "");
            }

        }
        tv_kucun.setText("库存"+bean.getSale_qty()+bean.getGoods_unit());
        tv_qiding.setText("起订量" + bean.getMin_buy_qty()+bean.getGoods_unit());
        tv_shop_name.setText(bean.getContact_person());
        tv_phone.setText(bean.getContact_tel());
        double d=Double.parseDouble(bean.getDiscount_price())*100;
        double d_qty=Double.parseDouble(bean.getMin_buy_qty());
        DecimalFormat df   = new DecimalFormat("######0");
        tv_sum.setText(df.format(d*d_qty));
        popupwindow.setWidth(LayoutParams.MATCH_PARENT);
        popupwindow.setHeight(LayoutParams.WRAP_CONTENT);
        popupwindow.setBackgroundDrawable(new BitmapDrawable());
        popupwindow.setFocusable(true);
        popupwindow.setOutsideTouchable(true);
        popupwindow.setContentView(dataSelectView);
        popupwindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (disMissListener != null) {
                    disMissListener.dissmiss();
                }
            }
        });
        tv_buy_count.setText(bean.getMin_buy_qty());
        tv_count_reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i_count = Integer.parseInt(tv_buy_count.getText().toString());
                String minbuy_qty=bean.getMin_buy_qty();
                int i_min_count=1;
                if(TextUtils.isEmpty(minbuy_qty)||Integer.parseInt(minbuy_qty)<1){
                    i_min_count=1;
                }else{
                    i_min_count=Integer.parseInt(minbuy_qty);
                }

                if (i_count > i_min_count) {
                    i_count = i_count -i_min_count;
                    tv_buy_count.setText(i_count + "");
                    double d = Double.parseDouble(bean.getDiscount_price())*100;
                    double new_d = d * i_count;
                    DecimalFormat df = new DecimalFormat("######0");
                    tv_sum.setText( df.format(new_d));
                }
            }
        });
        tv_count_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i_count=Integer.parseInt(tv_buy_count.getText().toString());
                int i_min_count=Integer.parseInt(bean.getMin_buy_qty());
                i_count=i_count+i_min_count;
                tv_buy_count.setText(i_count+"");
                double d=Double.parseDouble(bean.getDiscount_price())*100;
                double new_d=d*i_count;
                DecimalFormat df   = new DecimalFormat("######0");
                tv_sum.setText(df.format(new_d));

            }
        });
        bt_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(postListener!=null){
                    postListener.post(Integer.parseInt(tv_buy_count.getText().toString()),tv_sum.getText().toString().replace("¥",""));
                }
            }
        });
        if (!TextUtils.isEmpty(bean.getPic_info_url()) && !("{}".equals(bean.getPic_info_url()))) {
            Map map = Utils.getMapForJson(bean.getPic_info_url());
            Collection<String> co = map.values();
            String url = co.iterator().next();
            ImageLoader.getInstance().displayImage(url, img);
        }
        return popupwindow;
    }
    public  DisMissListener disMissListener;
    public interface DisMissListener{
        public void dissmiss();

    }
    public void setonDisMissListener(DisMissListener listener){
        this.disMissListener=listener;
    }

    public  PostListener postListener;
    public interface PostListener{
        public void post(int num, String money);

    }
    public void setonPostListener(PostListener postListener){
        this.postListener=postListener;
    }
}
